<?php
/**
 * User
 *
 * PHP version 5
 *
 * @category Controller
 * @version  1.0
 * @author   Duonght <hduongdmit@gmail.com>
 */
App::uses('AppController', 'Controller');
class NewsController extends AppController {

	/**
	* Components
	* @var array
	*/
	public $components = array(
		'Paginator',
		'RequestHandler',
		'ConstAdmin',
		'Session',
		'Common',
		'Captcha',
	);
	public $helpers = array('Text', 'Rss', 'Captcha');
	public $uses = array(
		'News',
		'Category',
		'Statics',
		'Faqs',
	);

	/**
	 * Chi tiết tin tức
	 * @param tring $cat_rewrite
	 * @param number $news_id
	 * @param string $news_rewrite
	 * @return a
	 */
	public function detail($cat_rewrite, $news_id, $news_rewrite){
		try {
			$cat_type = $this->Category->getCatType($cat_rewrite);
			switch ($cat_type) {
				case 'faq':
					$detail_news = self::getDetailFaqs($news_id);

					$categories = $this->Category->getParentId($detail_news['Faqs']['category_id']);
					foreach ($categories as $category) {
						$url = Router::url(array(
								'controller' => 'categorys',
								'action'=>'index' ,
								'cat_id' => $category['categories_multi']['id'],
								'cat_rewrite' => $category['categories_multi']['cat_rewrite'],
								'cat_parent_rewrite' => $category['categories_multi']['cat_parent_rewrite'],
						)
						);

						BreadcrumbComponent::set($category['categories_multi']['cat_name'], $url);
					}

					$title_for_layout = CommonComponent::cutstr($detail_news['Faqs']['question'], 60, false);
					$keywords_for_layout = $title_for_layout;
					$description_for_layout = CommonComponent::cutstr($detail_news['Faqs']['question'], 160, false);

					$this->set(
							array(
									'detail_news' => $detail_news,
									'title_for_layout' => $title_for_layout,
									'keywords_for_layout' => $keywords_for_layout,
									'description_for_layout' => $description_for_layout,
									'breadcrumb' => BreadcrumbComponent::create_links(),
							)
					);

					$this->render('detail_faq');
					break;
				case 'static':
					$detail_news = self::getDetailStatic($news_rewrite);

					$categories = $this->Category->getParentId($detail_news['Statics']['category_id']);
					foreach ($categories as $category) {
						$url = Router::url(array(
								'controller' => 'categorys',
								'action'=>'index' ,
								'cat_id' => $category['categories_multi']['id'],
								'cat_rewrite' => $category['categories_multi']['cat_rewrite'],
								'cat_parent_rewrite' => $category['categories_multi']['cat_parent_rewrite'],
							)
						);

						BreadcrumbComponent::set($category['categories_multi']['cat_name'], $url);
					}

					$title_for_layout = ($detail_news['Statics']['title_seo'] ? $detail_news['Statics']['title_seo'] : $detail_news['Statics']['sta_name']);
					$keywords_for_layout = $detail_news['Statics']['keyword_seo'];
					$description_for_layout = $detail_news['Statics']['description_seo'];

					$this->set(
							array(
									'detail_news' => $detail_news,
									'title_for_layout' => $title_for_layout,
									'keywords_for_layout' => $keywords_for_layout,
									'description_for_layout' => $description_for_layout,
									'breadcrumb' => BreadcrumbComponent::create_links(),
							)
					);

					$this->render('detail_static');

					break;
				default:
					$detail_news = self::getDetail($news_rewrite, $news_id);

					$categories = $this->Category->getParentId($detail_news['News']['category_id']);
					foreach ($categories as $category) {
						$url = Router::url(array(
								'controller' => 'categorys',
								'action'=>'index' ,
								'cat_id' => $category['categories_multi']['id'],
								'cat_rewrite' => $category['categories_multi']['cat_rewrite'],
								'cat_parent_rewrite' => $category['categories_multi']['cat_parent_rewrite'],
							)
						);

						BreadcrumbComponent::set($category['categories_multi']['cat_name'], $url);
					}

					$title_for_layout = ($detail_news['News']['title_seo'] ? $detail_news['News']['title_seo'] : $detail_news['News']['name']);
					$keywords_for_layout = $detail_news['News']['keyword_seo'];
					$description_for_layout = $detail_news['News']['description_seo'];

					$this->set(
							array(
									'detail_news' => $detail_news,
									'title_for_layout' => $title_for_layout,
									'keywords_for_layout' => $keywords_for_layout,
									'description_for_layout' => $description_for_layout,
									'breadcrumb' => BreadcrumbComponent::create_links(),
							)
					);
					break;
			}
		} catch(Exception $e) {
			throw new NotFoundException('Khong tim thay ban ghi nao trong bang News');
		}

	}

	/**
	 * Hien thi ma bao mat
	 */
	public function captcha()
	{
		$this->Captcha->configCaptcha(
				array(
						'width'=>200,//image width -default 200
						'height'=>70,//image height-default 70
						'quality'=>2,//set quality-high:3 ,mid:2 ,low:1
						'blur'=>false,//enablle/disable blur
						'format'=>'jpeg',//iage formats. jpeg or png
						'wordFile'=>false,//include word file or not
						'maxLength'=>5,//min. word length
						'minLength'=>5,//max word length
						//'background'=>array(87,87,87), //rgb color code here
						'color'=>array(
								array(128,0,255), //each color code in rgb in form of array
								array(0,255,255),
								array(255,0,0),
								//    true //uncomment if overwrite existing colors,comment if append new colors to existing ones
						),
						'pathType'=>2 //default:1.The Alternate path type    1 or 2.change pathtype to 2 if 1 does not work.
				)
		);

		$this->Captcha->getCaptcha();
	}

	private function getDetail($news_rewrite, $news_id){
		$options = array();
		$options['fields'] = array(
				'id',
				'name',
				'brief',
				'contents',
				'title_seo',
				'keyword_seo',
				'description_seo',
				'new_picture',
				'new_hits',
				'create_time',
				'category_id',
				'Category.id',
				'Category.cat_name',
				'Category.cat_rewrite',
		);
		$options['conditions'] = array(
				'active' => 1,
				'del_flg' => 0,
				'or' => array(
							'News.id' => $news_id,
							'new_md5' => md5(md5($news_rewrite) . Configure::read('Security.salt')),
						),
		);
		$options['joins'] = array(
				array(
						'table' => 'categories_multi',
						'alias' => 'Category',
						'type' => 'INNER',
						'conditions' => array(
								'News.category_id = Category.id'
						)
				)
		);

		$detail_news = $this->News->find('all', $options);
		$detail_news = reset($detail_news);

		//other news
		$other_news = array();
		$other = $this->News->query(
				"SELECT id,name,new_rewrite
					FROM news
					WHERE category_id = " . $detail_news['News']['category_id'] . "
					AND id != ". $detail_news['News']['id'] ." AND active = 1 AND del_flg = 0
					ORDER BY last_update DESC LIMIT 10");

// 		if ($detail_news == false){
// 			throw new NotFoundException('Khong tim thay ban ghi nao trong bang News');
// 		}

		$other_news['other_news'] = $other;

		//tăng lượt truy cập
		$new_hits = $detail_news['News']['new_hits'] + 1;
		$this->News->query("UPDATE news SET  new_hits = ". $new_hits ." WHERE id = ". $detail_news['News']['id']);

		return $detail_news + $other_news;
	}

	private function getDetailStatic($news_rewrite) {
		$options = array();
		$options['fields'] = array(
				'id',
				'sta_name',
				'sta_content',
				'title_seo',
				'keyword_seo',
				'description_seo',
				'create_time',
				'category_id',
				'Category.id',
				'Category.cat_name',
				'Category.cat_rewrite',
		);
		$options['conditions'] = array(
				'active' => 1,
				'del_flg' => 0,
				'md5' => md5(md5($news_rewrite) . Configure::read('Security.salt')),
		);
		$options['joins'] = array(
				array(
						'table' => 'categories_multi',
						'alias' => 'Category',
						'type' => 'INNER',
						'conditions' => array(
								'Statics.category_id = Category.id'
						)
				)
		);

		$detail_news = $this->Statics->find('all', $options);
		$detail_news = reset($detail_news);

		if ($detail_news == false){
			throw new NotFoundException('Khong tim thay ban ghi nao trong bang static');
		}

		return $detail_news;
	}

	private function getDetailFaqs($id) {
		$options = array();
		$options['fields'] = array(
				'id',
				'question',
				'question_rewrite',
				'answer',
				'name',
				'email',
				'question_hits',
				'create_time',
				'last_update',
				'category_id',
				'Category.id',
				'Category.cat_name',
				'Category.cat_rewrite',
		);
		$options['conditions'] = array(
				'active' => 1,
				'del_flg' => 0,
				'Faqs.id' => $id,
		);
		$options['joins'] = array(
				array(
						'table' => 'categories_multi',
						'alias' => 'Category',
						'type' => 'INNER',
						'conditions' => array(
								'Faqs.category_id = Category.id'
						)
				)
		);

		$detail_news = $this->Faqs->find('all', $options);
		$detail_news = reset($detail_news);

		if ($detail_news == false){
			throw new NotFoundException('Khong tim thay ban ghi nao trong bang static');
		}

		//tăng lượt truy cập
		$new_hits = $detail_news['Faqs']['question_hits'] + 1;
		$this->Faqs->query("UPDATE faqs SET question_hits = ". $new_hits ." WHERE id = ". $detail_news['Faqs']['id']);


		return $detail_news;
	}

}

